<?php

namespace app\admin\model;

use think\Model;
use think\Db;

class FilmstripModel extends Model
{
    protected $name = 'filmstrip';
    
    // 开启自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * [getFilmstripByWhere 根据搜索条件获取幻灯片列表信息]
     * @param $map
     * @param $Nowpage
     * @param $limits
     * @param $od
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getFilmstripByWhere($map, $Nowpage, $limits,$od)
    {
        return $this->where($map)->page($Nowpage, $limits)->order($od)->select();
    }

    /**
     * [getAllFilmstrip 获取全部幻灯片]
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllFilmstrip()
    {
        return $this->order('create_time desc')->select();
    }

    /**
     * [insertFilmstrip 添加幻灯片]
     * @param $param
     * @return array
     */
    public function insertFilmstrip($param)
    {
        Db::startTrans();// 启动事务
        try{
            $this->allowField(true)->save($param);
            Db::commit();// 提交事务
            writelog('幻灯片【'.$param['id'].'】添加成功',200);
            return ['code' => 200, 'data' => '', 'msg' => '幻灯片添加成功'];
        }catch( \Exception $e){
            Db::rollback();// 回滚事务
            writelog('幻灯片【'.$param['id'].'】添加失败',100);
            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '幻灯片添加失败'];
        }
    }

    /**
     * [editFilmstrip 编辑幻灯片]
     * @param $param
     * @return array
     */
    public function editFilmstrip($param)
    {
        Db::startTrans();// 启动事务
        try{
            $this->allowField(true)->save($param, ['id' => $param['id']]);
            Db::commit();// 提交事务
            writelog('幻灯片【'.$param['id'].'】编辑成功',200);
            return ['code' => 200, 'data' => '', 'msg' => '幻灯片编辑成功'];
        }catch( \Exception $e){
            Db::rollback();// 回滚事务
            writelog('幻灯片【'.$param['id'].'】编辑失败',100);
            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '幻灯片编辑失败'];
        }
    }

    /**
     * [getOneFilmstrip 根据幻灯片id获取一条信息]
     * @param $id
     * @return array|bool|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneFilmstrip($id)
    {
        return $this->where('id', $id)->find();
    }

    /**
     * [delFilmstrip 删除幻灯片]
     * @param $id
     * @return array
     */
    public function delFilmstrip($id)
    {
        Db::startTrans();// 启动事务
        try{
            $this->where('id', $id)->delete();
            Db::commit();// 提交事务
            writelog('幻灯片【'.$id.'】删除成功',200);
            return ['code' => 200, 'data' => '', 'msg' => '幻灯片删除成功'];
        }catch( \Exception $e){
            Db::rollback();// 回滚事务
            writelog('幻灯片【'.$id.'】删除失败',100);
            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '幻灯片删除失败'];
        }
    }

    /**
     * [batchDelFilmstrip 批量删除幻灯片]
     * @param $param
     * @return array
     */
    public function batchDelFilmstrip($param){
        Db::startTrans();// 启动事务
        try{
            FilmstripModel::destroy($param);
            Db::commit();// 提交事务
            writelog('幻灯片批量删除成功',200);
            return ['code' => 200, 'data' => '', 'msg' => '批量删除成功'];
        }catch( \Exception $e){
            Db::rollback();// 回滚事务
            writelog('幻灯片批量删除失败',100);
            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量删除失败'];
        }
    }
    /**
     * [filmstripState 幻灯片状态]
     * @param $param
     * @return array
     */
    public function filmstripState($id,$num)
    {
        if($num == 2){
            $msg = '禁用';
        }else{
            $msg = '启用';
        }
        Db::startTrans();// 启动事务
        try {
            $this->where ('id' , $id)->setField (['status' => $num]);
            Db::commit();// 提交事务
            writelog ( '幻灯片【' . $id . '】'.$msg.'成功' , 200);
            return ['code' => 200 , 'data' => '' , 'msg' => '已'.$msg];
        } catch (\Exception $e) {
            Db::rollback();// 回滚事务
            writelog ( '幻灯片【' . $id . '】'.$msg.'失败' , 100);
            return ['code' => 100 , 'data' => $e->getMessage(), 'msg' => $msg.'失败'];
        }
    }

    /**
     * [forbiddenFilmstrip 批量禁用幻灯片]
     * @param $param
     * @return array
     */
    public function forbiddenFilmstrip($param){
        Db::startTrans();// 启动事务
        try{
            if($param){
                $this->saveAll($param);
            }else{
                $this->where('1=1')->update(['status'=>2]);
            }
            Db::commit();// 提交事务
            writelog('批量禁用幻灯片成功',200);
            return ['code' => 200, 'data' => '', 'msg' => '批量禁用成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务
            writelog('批量禁用幻灯片失败',100);
            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量禁用失败'];
        }
    }

    /**
     * [usingFilmstrip 批量启用幻灯片]
     * @param $param
     * @return array
     */
    public function usingFilmstrip($param){
        Db::startTrans();// 启动事务
        try{
            if($param){
                $this->saveAll($param);
            }else{
                $this->where('1=1')->update(['status'=>1]);
            }
            Db::commit();// 提交事务
            writelog('批量启用幻灯片成功',200);
            return ['code' => 200, 'data' => '', 'msg' => '批量启用成功'];
        }catch( \Exception $e){
            Db::rollback ();//回滚事务
            writelog('批量启用幻灯片失败',100);
            return ['code' => 100, 'data' => $e->getMessage(), 'msg' => '批量启用失败'];
        }
    }

}